// pages/integralMall/integralMall.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    article: [
      { id: "177", title: "新零售的未来有哪些基本特征？" }
    ],
    tab: 1,  //商品优惠券切换
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    if (app.globalData.userId) {
      wx.showLoading({ title: '加载中...' });
      wx.request({
        url: app.baseURL + 'Integral&a=index',
        data: {
          user_id: app.globalData.userId
        },
        success: (res) => {
          if (res.statusCode == 200) {
            this.setData({
              info: res.data,
            });
            wx.hideLoading();
          }
        }
      });
    } else {
      app.toLogin();
    }
    //执行文字滚动动画
    this.scroll();
  },

  //文字滚动
  scroll() {
    let list = this.data.article; //获取数据列表
    clearInterval(this.Interval);
    //3s滚动一次
    this.Interval = setInterval(() => {
      this.setData({
        isTop: true
      });
      //tanslation = 300ms，故数据变动的间隔也是300ms
      setTimeout(() => {
        let list = this.data.article;
        list.push(list.shift());
        this.setData({
          article: list,
          isTop: false
        });
      }, 300);
    }, 3000);
  },

  //商品优惠券切换
  tabswitch (e) {
    this.setData({
      tab: e.currentTarget.dataset.tab
    })
  },


  //兑换优惠券
  getIntegral(e) {
    let tath = this;
    const yhq_id = e.currentTarget.dataset.id; //优惠券id
    wx.request({
      url: app.baseURL + 'Index&a=change',
      data: {
        user_id: app.globalData.userId,
        yhq_id: yhq_id
      },
      success: (res) => {
        if (res.data.status == 1) {
          wx.showModal({
            title: '',
            content: res.data.info,
            success: function (res) {
              if (res.confirm) {
                wx.request({
                  url: app.baseURL + 'Index&a=coupon_get',
                  data: {
                    user_id: app.globalData.userId,
                    yhq_id: yhq_id,
                    ask: 1
                  },
                  success: (res) => {
                    wx.showToast({
                      title: '兑换成功',
                      icon: 'none',
                    })
                  }
                });
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          // 
        } else if (res.data.status == 0) {
          wx.showToast({
            title: '您的积分不足',
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },

})